<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');

if ( !ISADMIN && !ican('link') ) msg('对不起，你没有权限执行本操作！');

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
$m = new LinksModule;
$m->$f();
class LinksModule
{
	function index() {//列表
		global $_G;
		$wheresql = '';
		$page = max(1, getGP('page','G','int'));
		$pagesize = 20;
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?m=link';
	
		if ($keyword = getGP('keyword','G')) {
			$wheresql .= " AND (l.title LIKE '%$keyword%' OR l.url LIKE '%$keyword%')";
			$url .= '&keyword='.rawurlencode($keyword);
		}
		if (getGP('visible','G') == 'yes') {
			$wheresql .= " AND l.visible = 1";
			$url .= '&visible=yes';		
		} elseif (getGP('visible','G') == 'no') {
			$wheresql .= " AND l.visible = 0";
			$url .= '&visible=no';		
		}
		if ($cid = getGP('cid','G','int')) {
			$wheresql .= " AND l.cid = $cid";
			$url .= '&cid='.$cid;	
		}
	
		$num = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."link l WHERE 1 $wheresql");
	
		$sql = "SELECT l.*,c.catename FROM ".DB_PREFIX."link l LEFT JOIN ".DB_PREFIX."category c ON l.cid = c.cid WHERE 1 $wheresql ORDER BY l.cid ASC,l.listorder ASC LIMIT $offset, $pagesize";
		$result = $_G['db']->fetch_all($sql);
	
		include admintemplate('link');
	}
	function add(){
		global $_G;
		if (!$_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."category WHERE type=1")) {
			msg('当前还没有链接分类，请先添加一个分类。');
		}
		$orderid = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."link");
		include admintemplate('link_add');
	}
	function save(){
		global $_G;
		$savetype = getGP('savetype','P');
		$id = getGP('id','P','int');
		$link = array(
			'cid' => getGP('cid','P','int'),
			'title' => check_str(getGP('title','P')),
			'url' => getGP('url','P'),
			'rssurl' => getGP('rssurl','P'),
			'logo' => check_str(getGP('logo','P')),
			'summary' => check_str(getGP('summary','P')),
			'visible' => getGP('visible','P','int')
		);
		if ($savetype == 'add') {
			$link['dateline'] = TIMESTAMP;
			if ($_G['db']->insert('link',$link)) {
				$_G['db']->query("UPDATE ".DB_PREFIX."category SET total=total+1 WHERE cid=".$link['cid']);
				recache('link,category');
				msg('成功添加一个链接', 'admin.php?m=link');
			}
		} elseif ($savetype) {
			$_G['db']->update('link',$link, array('lid'=>$id));
			recache('link');
			msg('成功编辑一个链接', 'admin.php?m=link');
		}
	}
	function edit(){
		global $_G;
		$id = getGP('id','G','int');
		if ($result = $_G['db']->fetch_one_array("SELECT * FROM ".DB_PREFIX."link WHERE lid = '$id'")) {
			include admintemplate('link_edit');
		} else {
			msg('链接不存在');
		}
	}
	function delete(){
		global $_G;
		$idarr = getGP('id','P','array');
		foreach ($idarr as $id) {
			$_G['db']->query("DELETE FROM ".DB_PREFIX."link WHERE lid = '$id'");
		}
		recache('link');
		msg('成功删除选中的链接',$_G['referurl']);
	}
	function move(){
		global $_G;
		$cid = getGP('cid','P','int');
		$idarr = getGP('id','P','array');
		foreach ($idarr as $id) {
			$_G['db']->query("UPDATE ".DB_PREFIX."link SET cid = '$cid' WHERE lid = '$id'");
		}
		recache('link');
		msg('成功移动选中的链接',$_G['referurl']);
	}
	function resort(){
		global $_G;
		$idarr = getGP('orderid','P','array');
		foreach ($idarr as $id => $orderid) {
			$_G['db']->query("UPDATE ".DB_PREFIX."link SET listorder = '$orderid' WHERE lid = '$id'");
		}
		recache('link');
		msg('成功更新排序',$_G['referurl']);
	}
}
?>